<template>
    <div class="portlet box blue">
        <div class="portlet-title">
            <div class="caption">
                <i class="fa fa-gift"></i>比赛查询</div>
            <div class="tools">
                <a href="javascript:;" class="collapse" data-original-title="" title=""></a>
            </div>
        </div>
        <div class="portlet-body form">
            <!-- BEGIN FORM-->
            <form action="#" class="form-horizontal">
                <div class="form-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="control-label col-md-3">通行证号</label>
                                <div class="col-md-9">
                                    <passport-select ref="passportSelect" @PassportSelectChanged="passportSelectChanged"></passport-select>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="control-label col-md-3">比赛状态</label>
                                <div class="col-md-9">
                                    <el-select v-model="condition.States" multiple clearable placeholder="请选择比赛项目状态" style="width:100%">
                                        <el-option v-for="item of words.states" :key="item.code" :label="item.text" :value="item.code"></el-option>
                                    </el-select>                                    
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="control-label col-md-3">比赛项目</label>
                                <div class="col-md-9">
                                    <el-select v-model="condition.Events" multiple clearable placeholder="请选择比赛项目条件" style="width:100%">
                                        <el-option v-for="(word, index) of words.events" :key="index" :label="word" :value="index"></el-option>
                                    </el-select>                                    
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="control-label col-md-3">比赛组别</label>
                                <div class="col-md-9">
                                    <el-select v-model="condition.Groups" multiple clearable placeholder="请选择比赛项目条件" style="width:100%">
                                        <el-option v-for="(word, index) of words.groups" :key="index" :label="word" :value="index"></el-option>
                                    </el-select>
                                </div>
                            </div> 
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="control-label col-md-3">伴奏形式</label>
                                <div class="col-md-9">
                                    <el-input placeholder="请输入您想查询的比赛伴奏类型" v-model="condition.Accompany" clearable></el-input>
                                </div>
                            </div> 
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label class="control-label col-md-3">创建日期</label>
                                <div class="col-md-9">
                                    <el-date-picker v-model="createTimeRange" type="daterange" align="right" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" style="width:100%"></el-date-picker>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="form-actions">
                    <div class="row">
                        <div class="col-md-12 pull-right">
                            <div style="float:right;">
                                <button type="button" class="btn green" @click="exportCompetes()">导出比赛列表</button>
                                <button type="button" class="btn green" @click="query()">查询比赛</button>
                                <button type="button" class="btn default" @click="clear()">清空条件</button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
            <!-- END FORM-->
        </div>
    </div>
</template>

<script>

import {GetPickerShortcuts} from 'web/utils.js';
import PassportSelect from 'components/passport_select.vue';

export default {
    props: {
        'data': {
            type: Object,
            default: null
        },
    },
    components: {
        PassportSelect
    },
    data() {
        return {
            condition: {
                States: [0, 1],
                PlayerCount0: 1,
                PlayerCount1: 1
            },
            createTimeRange: null,
            words: {
                events: {},
                groups: {},
                states: [{
                    text: '已删除',
                    code: -1
                }, {
                    text: '未激活',
                    code: 0
                }, {
                    text: '已激活',
                    code: 1
                }]
            },
            pickerOptions: GetPickerShortcuts(),
        }
    },
    watch: {
        'data': {
            handler: function(value){
                this.condition = value;
            },
            deep: true
        },
    },
    mounted() {
        // 加载词语
        this.$DataService.GetWordDictByCode('COMPETE_GROUP')
            .then(ret => {
                this.words.groups = ret;
            });
        this.$DataService.GetWordDictByCode('SOLO_COMPETE_EVENT')
            .then(ret => {
                this.words.events = ret;
            });
        
        // 执行查询
        this.query();
    },
    methods: {
        query() {
            // 将时间范围分解到condition中
            let _condition = Object.assign({}, this.condition);
            if (this.createTimeRange) {
                _condition.CreateTime0 = this.createTimeRange[0];
                _condition.CreateTime1 = this.createTimeRange[1];
            }
            this.$emit('getPassportCondition', _condition);
        }, 

        passportSelectChanged(userIds) {
            this.condition.UserIds = userIds;
        },

        clear() {
            this.condition = {
                PlayerCount0: 1,
                PlayerCount1: 1,
                States: [],
                Groups: [],
                Events: []
            };
            this.createTimeRange = null;          
        },

        exportCompetes() {
            this.query();            
            this.$CompeteService.ExportCompetes(this.condition);
        }
    }
}
</script>

<style>

</style>
